package tt.dz.egobus.servicemanager.bean;

import com.alibaba.fastjson.JSON;
import tt.dz.egobus.bean.BaseBean;
import tt.dz.egobus.servicemanager.vo.SystemUserVo;

import java.util.Date;

/**
 * @author Anson
 * @date 17-3-30
 * @name session vo
 * @since 1.0.3
 */
public class UserSession implements BaseBean {

    private static final long serialVersionUID = 1121153143181976267L;

    public static final UserSession jsonToBean(String json) {
        return JSON.parseObject(json, UserSession.class);
    }

    public static final UserSession instance(SystemUserVo vo, long timeout) {
        UserSession session = new UserSession();
        session.setExpire(timeout);
        session.updateSession();
        session.setUserId(vo.getId());
        session.setUserName(vo.getUsername());
        return session;
    }

    public UserSession() {

    }


    public final String toJson() {
        return JSON.toJSONString(this);
    }

    /**
     * 更新session
     */
    public void updateSession() {
        this.lastestTime = new Date();
    }


    /**
     * @author Anson
     * @date 17-3-30
     * @name 是否过期
     * @since 1.0.3
     */
    public boolean isExpire() {
        return (System.currentTimeMillis() - this.lastestTime.getTime()) > this.expire;
    }

    /**
     * 用户id
     */
    private String userId;

    /**
     * 用户名
     */
    private String userName;

    /**
     * 最后一次更新时间
     */
    private Date lastestTime;

    /**
     * 用户类别
     */
    private int userType;

    /**
     * 当前超时时间 毫秒数
     */
    private long expire;


    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public Date getLastestTime() {
        return lastestTime;
    }

    public void setLastestTime(Date lastestTime) {
        this.lastestTime = lastestTime;
    }

    public int getUserType() {
        return userType;
    }

    public void setUserType(int userType) {
        this.userType = userType;
    }

    public long getExpire() {
        return expire;
    }

    public void setExpire(long expire) {
        this.expire = expire;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof UserSession)) return false;

        UserSession that = (UserSession) o;

        if (getUserType() != that.getUserType()) return false;
        if (getExpire() != that.getExpire()) return false;
        if (getUserId() != null ? !getUserId().equals(that.getUserId()) : that.getUserId() != null) return false;
        if (getUserName() != null ? !getUserName().equals(that.getUserName()) : that.getUserName() != null)
            return false;
        return getLastestTime() != null ? getLastestTime().equals(that.getLastestTime()) : that.getLastestTime() == null;

    }

    @Override
    public int hashCode() {
        int result = getUserId() != null ? getUserId().hashCode() : 0;
        result = 31 * result + (getUserName() != null ? getUserName().hashCode() : 0);
        result = 31 * result + (getLastestTime() != null ? getLastestTime().hashCode() : 0);
        result = 31 * result + getUserType();
        result = 31 * result + (int) (getExpire() ^ (getExpire() >>> 32));
        return result;
    }

    @Override
    public String toString() {
        return "UserSession{" +
                "userId='" + userId + '\'' +
                ", userName='" + userName + '\'' +
                ", lastestTime=" + lastestTime +
                ", userType=" + userType +
                ", expire=" + expire +
                '}';
    }
}
